﻿--[[
   @brief    game library control
   @author   phenixwu
   @version  1.0
   @date     2015-03-13
--]]

game_library_control = {
    -- type = mmo_game用
    sub_page2id_url_table = {
        GAME_DETAIL = {
            id = GAME_ID.GAME_ID_SHOP;
            -- url = "http://plat.tgp.qq.com/center/gamelibrary/MMOG_detail.html";
            url = LoadStr("GL_GAME_DETAIL_URL");
        };
        ALL_GAME = {
            id = GAME_ID.GAME_ID_SHOP;
            --url = "http://plat.tgp.qq.com/center/gamelibrary/game_index.html";
            url = LoadStr("GL_ALL_GAME_URL");
        };
        NETBAR = {
            id = GAME_ID.GAME_ID_TGP;
            url = LoadStr("GL_NETBAR_URL");
        };
        MMOG = {
            id = GAME_ID.GAME_ID_SHOP;
            url = LoadStr("GL_MMOG_URL");
        };
        WEB_GAME = {
            id = GAME_ID.GAME_ID_SHOP;
            url = LoadStr("GL_WEB_GAME_URL");
        };
        MOBILE_GAME = {
            id = GAME_ID.GAME_ID_SHOP;
            url = LoadStr("GL_MOBILE_GAME_URL");
        };
        CONSOLE_GAME = {
            id = GAME_ID.GAME_ID_SHOP;
            --url = "http://plat.tgp.qq.com/center/gamelibrary/console_game.html";
            url = LoadStr("GL_CONSOLE_GAME_URL");
        };
        HOME_PAGE = {
            id = GAME_ID.GAEM_ID_HOME_PAGE;
            --url = "http://tgp.qq.com/client_index_v3.shtml";
            url = LoadStr("GL_HOMEPAGE_URL");
        };
    };
    
    init = function()
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        --[[ lcu remove
        tpf_ui:RegisterJsEvent("m_get_lol_version_info", "game_library_control.get_lol_version_info");
        ]]
        event_center.add_listener(extendboard_event.ON_JS_CALLBACK_GL, 'game_library_control.on_js_callback');
        event_center.add_listener(extendboard_event.OPEN_GAME_DETIAL, 'game_library_control.update_game_detail_info_ex');
        event_center.add_listener(extendboard_event.OPEN_GAME_LIST, 'game_library_control.update_game_list_ex');
        event_center.add_listener(extendboard_event.UPDATE_GAMELIBRARY_ALPHA, 'game_library_control.update_gamelibrary_alpha');
        event_center.add_listener(extendboard_event.QUERY_PLAY_GAME_LIST, 'game_library_control.query_play_game_list');
        event_center.add_listener(extendboard_event.QUERY_ATTENT_GAME_LIST, 'game_library_control.query_attent_game_list');
        event_center.add_listener(extendboard_event.QUERY_NEAREST_GAME_LIST, 'game_library_control.query_nearest_game_list');
        event_center.add_listener(extendboard_event.QUERY_REC_GAME_NAME, 'game_library_control.query_rec_game_name');
        event_center.add_listener(extendboard_event.QUERY_EXPECT_GAME_RANK, 'game_library_control.query_expect_game_rank');
        event_center.add_listener(extendboard_event.QUERY_GAME_INFO_BY_ID, 'game_library_control.query_game_info_by_id');
        event_center.add_listener(extendboard_event.QUERY_GAME_INFO_BY_ID_V2, 'game_library_control.query_game_info_by_id_v2');
        event_center.add_listener(extendboard_event.JC_GET_FIRST_OPEN_DETAIL, 'game_library_control.get_first_open_detail');
        -- event_center.add_listener(extendboard_event.JC_SET_FIRST_OPEN_DETAIL, 'game_library_control.set_first_open_detail');
        event_center.add_listener(extendboard_event.SHOW_CUSTOM_COVER_TIPS, 'game_library_control.ShowCustomCoverTips');
        comm_center.reg_service("SvrLuaGameLib_QueryGameInfo", game_library_control.on_query_game_info);
        comm_center.reg_service("Svr_RailHost_ShowUpdateDlcFailed", game_library_control.on_show_dlc_update_failed);
        ierd_tgp.tpf_ui.Require('game_library\\model.lua');
        ierd_tgp.tpf_ui.Require('game_library\\view.lua');

        ierd_tgp.tpf_ui.Require('web_bottom\\model.lua');
        
        base_utility.log("[game_library_control] init success.");
    end;
   
    
    init_game_base_info = function()
        game_library_control.update_game_base_info();
        game_library_model.game_base_info_ready = true;
    end;

    update_game_base_info = function()
        event_center.send_event(banner_event.UPDATE_GAME_BASE_INFO, "");
    end;
    
    reserved_fun = function()
    end;

    --[[ lcu remove
    -- special for lol lcu version
    get_lol_version_info = function (func_name, json, browser_id)
        if func_name ~= "m_get_lol_version_info" then
            return;
        end
        base_utility.log(string.format("[game_library_control]get_lol_version_info: %s", json));
        
        local param = base_utility.json_to_table(json);
        game_library_model.lcu_req_param = json;
        game_library_model.lcu_plugin_update_browser_id = browser_id;
        if param ~= nil then
            local game_lib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
            local lol_lcu_table = web_utility.web_rsp_comm_data(param, 0, "");
            local game_id = 26;
            local game_servers = banner_model.get_game_servers(game_id);
            if game_servers ~= nil 
                and game_servers.selleted ~= 0 then
                if nil ~= game_lib then 
                    game_lib:UpdateLcuServerSelected(game_id, game_servers.selleted);
                end
            end
            lol_lcu_table.data = {};
            if nil ~= game_lib then
                local lcu_state = ierd_tgp.game_library.LCUState:new();
                game_lib:GetLCUState(lcu_state);
                lol_lcu_table.data.air_ava = lcu_state.air_available;
                lol_lcu_table.data.is_ava = lcu_state.lcu_available;
                lol_lcu_table.data.is_lcu = lcu_state.using_lcu;
                lol_lcu_table.data.is_lcu_merge_air = lcu_state.is_lcu_merge_air;
                lol_lcu_table.data.is_lcu_full = lcu_state.is_lcu_full;
                lol_lcu_table.data.is_open_fallback = lcu_state.is_open_fallback;
                lol_lcu_table.data.is_lcu_server_launch = lcu_state.is_lcu_server_launch;
                lol_lcu_table.data.selected_ver_fallback = lcu_state.selected_ver;
                
                -- 7.4版本回退开关设置 针对只安装7.4版本 调整启动按钮
                if lol_lcu_table.data.is_ava == false  and lol_lcu_table.data.is_lcu_merge_air == true then 
                    if lol_lcu_table.data.is_open_fallback == false then --版本回退未开启 
                        if lol_lcu_table.data.is_lcu_server_launch == true then -- 选择非灰度大区 --不显示按钮切换
                            lol_lcu_table.data.is_ava = true -- 假装LCU独立客户端可用
                            lol_lcu_table.data.is_lcu = true -- lcu可用
                            lol_lcu_table.data.air_ava = false 
                        else
                            lol_lcu_table.data.is_ava = false -- 假装LCU独立客户端不可用
                            lol_lcu_table.data.is_lcu = false 
                            lol_lcu_table.data.air_ava = true -- air可用
                        end
                    else -- 开启7.4版本回退： 灰度大区支持两种启动方式 两种客户端可用
                        if lol_lcu_table.data.is_lcu_server_launch == true then -- 选择非灰度大区 --不显示按钮切换
                            lol_lcu_table.data.air_ava = true
                            lol_lcu_table.data.is_ava = true -- 默认使用新客户端
                            if lcu_state.selected_ver == 1 then
                                lol_lcu_table.data.is_lcu = true
                            else
                                lol_lcu_table.data.is_lcu = false
                            end
                        else
                            lol_lcu_table.data.air_ava = true
                            lol_lcu_table.data.is_ava = false -- 默认使用旧客户端
                            lol_lcu_table.data.is_lcu = false
                        end
                    end
                end
                
                lcu_state:delete();
            end
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local lol_lcu_json = base_utility.table_to_json(lol_lcu_table);
                web_utility.lua_call_js_by_id_ex(param.callback_func, lol_lcu_json, browser_id);
            end
        end
    end;
    -- end
    ]]

    --[[ lcu remove
    update_lcu_version_info  = function () 
        local param = base_utility.json_to_table(game_library_model.lcu_req_param);
        if param ~= nil then
            local game_lib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
            local lol_lcu_table = web_utility.web_rsp_comm_data(param, 0, "");
            lol_lcu_table.data = {};
            if nil ~= game_lib then
                local lcu_state = ierd_tgp.game_library.LCUState:new();
                game_lib:GetLCUState(lcu_state);
                lol_lcu_table.data.air_ava = lcu_state.air_available;
                lol_lcu_table.data.is_ava = lcu_state.lcu_available;
                lol_lcu_table.data.is_lcu = lcu_state.using_lcu;
                lol_lcu_table.data.is_lcu_merge_air = lcu_state.is_lcu_merge_air;
                lol_lcu_table.data.is_lcu_full = lcu_state.is_lcu_full;
                lol_lcu_table.data.is_open_fallback = lcu_state.is_open_fallback;
                lol_lcu_table.data.is_lcu_server_launch = lcu_state.is_lcu_server_launch;
                lol_lcu_table.data.selected_ver_fallback = lcu_state.selected_ver_fallback;
                -- 未开启7.4版本回退开关时
                if lol_lcu_table.data.is_ava == false then 
                    if lol_lcu_table.data.is_open_fallback == false then
                        if lol_lcu_table.data.is_lcu_merge_air == true 
                            and lol_lcu_table.data.is_lcu_server_launch == true then
                            -- 使用7.4版本，当前选择大区支持启动lcu，且没有开启回滚，强制显示新客户端按钮
                            lol_lcu_table.data.is_ava = true -- 假装LCU独立客户端可用
                            lol_lcu_table.data.is_lcu = true 
                            lol_lcu_table.data.air_ava = false -- 关闭切换
                        end
                    else -- 开启7.4版本回退： 灰度大区支持两种启动方式 两种客户端可用
                        if lol_lcu_table.data.is_lcu_merge_air == true 
                            and lol_lcu_table.data.is_lcu_server_launch == true then
                            lol_lcu_table.data.air_ava = true
                            lol_lcu_table.data.is_ava = true -- 默认使用旧客户端
                            -- 根据本地记录下灰度大区选择客户端配置->显示按钮
                            if lol_lcu_table.data.selected_ver_fallback == 0 then
                                lol_lcu_table.data.is_lcu = true
                            end
                            if lol_lcu_table.data.selected_ver_fallback == 1 then
                                lol_lcu_table.data.is_lcu = false
                            end
                        end
                    end
                end
                
                lcu_state:delete();
            end
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local lol_lcu_json = base_utility.table_to_json(lol_lcu_table);
                web_utility.lua_call_js_by_id_ex(param.callback_func, 
                    lol_lcu_json, game_library_model.lcu_plugin_update_browser_id);
            end
        end
    end;
    ]]
    
    get_game_list = function(main_type, category, sub_type, sort)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local game_category = ierd_tgp.game_library.GameCategory:new_local();
        gl_adapter:get_game_category(main_type, category, sub_type, sort, game_category);
        base_utility.log("[game_library_control] get_game_list "..game_category.game_count);
        return game_category;
    end;

    is_game_in_game_list = function(data_in)
        local is_installed = false;

        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        local temp_list = ierd_tgp.game_library.GameCategory:new();
        if game_lib ~= nil then
            game_lib:get_game_category(0, 10, 0, 0, temp_list);
            local count = temp_list.game_count;
            for i = 1, count do 
                if temp_list.game_list[i - 1] == data_in.game_id then
                    is_installed = true;
                    break
                end
            end
        end
        temp_list:delete();

        return {is_installed = is_installed};
    end;
    
    generate_game_info = function(game_list)
        game_list_info = {};
        
        local i = 0;
        local j = 0;
        for i = 0, game_list.game_count - 1, 1 do
            local game_id = game_list.game_list[i];
            local game_detail_info = banner_model.get_game_base_info(game_id);
            if game_detail_info ~= nil and game_detail_info.type ~= game_library_model.MOBILE_GAME_TYPE then
                local game_info = {};
                game_info.game_id = game_id;
                game_info.game_icon = game_detail_info.poster_url;
                game_info.game_name = game_detail_info.name;
                game_info.game_main_type = game_detail_info.main_type;
                game_info.game_type = game_detail_info.type;
                game_info.game_sub_type = game_detail_info.sub_type;
                game_info.distribution_channels = game_detail_info.distribution_channels;
                game_info.game_desc = game_detail_info.slogan;
                game_info.game_new = game_detail_info.is_new;
                game_info.alpha = game_detail_info.name_first_letter;
                game_info.name_type = game_detail_info.name_type;
                game_info.game_installed = game_detail_info.is_installed;
                game_info.game_tags = game_detail_info.game_tags;
                game_info.tags = game_detail_info.tags;
                game_info.comments = game_detail_info.comments;
                game_info.icon_url = game_detail_info.icon_url;
                game_info.release_date = game_detail_info.release_date;
                game_info.popularity = game_detail_info.popularity;

                local detail_info = game_library_control.get_game_detail_info(game_id);
                if nil ~= detail_info and nil ~= detail_info.game_install_state then
                    game_info.game_install_state = detail_info.game_install_state;
                end

                game_list_info[j] = game_info;
                j = j + 1;
            end
        end
        
        return game_list_info;
    end;
    
    update_game_list = function(main_type, category, sub_type, sort, alpha)
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end
       
        local game_list, hold = game_library_control.get_game_list(main_type, category, sub_type, sort);
        local json_str = {};
        if game_library_model.mmog_page == true then
            game_library_model.main_type = main_type;
            game_library_model.category = category;
            game_library_model.sub_type = sub_type;
            game_library_model.sort = sort;
            game_library_model.alpha = alpha;
            game_library_model.mmog_page = false;
        end
        game_library_model.current_select_game_id = 0; -- mark current page
        json_str.category = category;
        json_str.sort = sort;
        json_str.alpha = alpha;
        event_center.send_event(extendboard_event.ON_EXTENDBOARD_CATEGORY_CHANGE, 
            base_utility.table_to_json(json_str));
        if category == 3 then  --对于特权游戏不更新游戏列表，直接返回
            return;
        end
        local game_list_info = game_library_control.generate_game_info(game_list);
        local const_json_str, hold_json_str = base_utility.table_to_json(game_list_info);
        web_utility.lua_call_js('update_game_list', const_json_str, CID_MAINFRAME_QBWEB);
        base_utility.log("[game_library_control]update_game_list success.");
    end;
    
    update_game_list_ex = function(event_id, json)
        local param = base_utility.json_to_table(json);
        local main_type = game_library_model.main_type;
        local category = game_library_model.category;
        local sub_type = game_library_model.sub_type;
        local sort = game_library_model.sort;
        local alpha = game_library_model.alpha;
        if param.main_type ~= nil then
            main_type = param.main_type;
        end
        if param.alpha ~= nil then
            alpha = param.alpha;
        end
        if param.category ~= nil then
            category = param.category;
        end
        if param.sub_type ~= nil then
            sub_type = param.sub_type;
        end
        game_library_model.mmog_page = true;
        game_library_control.update_game_list(main_type, category, sub_type, sort, alpha);

        local jstr_table = {};
        jstr_table.category = category;
        jstr_table.sort = sort;
        jstr_table.alpha = alpha;
        local jstr = base_utility.table_to_json(jstr_table);
        if category ~= nil then
            web_utility.lua_call_js("switch_game_category", jstr, CID_MAINFRAME_QBWEB);
        end;
    end;
    
    update_gamelibrary_alpha = function(event_id, json)
        local param = base_utility.json_to_table(json);
        if param ~= nil and param.alpha ~= nil then
            game_library_model.alpha = param.alpha;
        end

        local jstr_table = {};
        jstr_table.alpha = alpha;
        local jstr = base_utility.table_to_json(jstr_table);
        if param ~= nil and param.func_callback ~= nil and param.func_callback ~= "" then
            web_utility.lua_call_js(param.func_callback, jstr, CID_MAINFRAME_QBWEB);
        end;
    end;

    update_game_privilege = function()
        local pc_adapter =
            base_utility.get_component_adapter("IPersonal_center",
                "ierd_tgp.personal_center.GetPersonalCenterModule();");
        pc_adapter:getAllPrivInfo();
    end;
    
    get_game_detail_info = function(game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local game_detail_info = ierd_tgp.game_library.GameDetailInfo:new_local();
        gl_adapter:get_game_detail_info(game_id, game_detail_info);
        
        return game_detail_info;
    end;

    get_bind_channel_id = function()
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        return gl_adapter:get_bind_channel_id();
    end;
    
    get_game_privilege = function(game_id)
        local privilege = {};
        local index = 1;
        if personal_center_model ~= nil and personal_center_model.my_privilege_list ~= nil then
            if personal_center_model.my_privilege_list.game_privilege_list ~= nil then
                for key, value in ipairs(personal_center_model
                    .my_privilege_list
                    .game_privilege_list) do
                    if game_id == value.real_gameid then
                        for k, v in ipairs(value.desc) do 
                            if v ~= "" then
                                privilege[index] = v;
                                index = index + 1;
                            end
                        end
                    end
                end
            end
        end
        return privilege;
    end;

    get_game_privilege_total_info = function(game_id)
        local info = {};
        local index = 1;
        if personal_center_model ~= nil and personal_center_model.my_privilege_list ~= nil then
            if personal_center_model.my_privilege_list.game_privilege_list ~= nil then
                for key, value in ipairs(personal_center_model
                    .my_privilege_list
                    .game_privilege_list) do
                    if game_id == value.real_gameid then
                        info = value;
                    end
                end
            end
        end
        return info;
    end;

    get_game_detial_info_service = function(data_in)
        if data_in == nil or data_in.game_id == nil then
            base_utility.log("[game_library_control] param is nil:"..base_utility.table_to_json(data_in));
            return;
        end

        if nil == personal_center_model or nil == personal_center_model.my_privilege_list or
           nil == personal_center_model.my_privilege_list.game_privilege_list then
            game_library_control.update_game_privilege();
        end;
        
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        -- 之前是查询一个game_id，现在兼容查询列表
        if type(data_in.game_id) == "table" then -- 传递的是id列表
            local game_detail_table_list = {};
            local game_detail_info = ierd_tgp.game_library.GameDetailInfo:new_local();
            for index, game_item in ipairs(data_in.game_id) do
                gl_adapter:get_game_detail_info(tonumber(game_item.game_id), game_detail_info); 
                local game_detail_table = game_library_control.generate_game_detail_info(game_detail_info);
                if tonumber(game_detail_table.game_id) == tonumber(game_item.game_id) then
                    game_detail_table_list[tostring(game_item.game_id)] = game_detail_table;
                end                    
            end
            return game_detail_table_list;

        else -- 老的查询单个game_id
            local game_detail_info = ierd_tgp.game_library.GameDetailInfo:new_local();
            gl_adapter:get_game_detail_info(data_in.game_id, game_detail_info);
            local game_detail_table = game_library_control.generate_game_detail_info(game_detail_info);
            return game_detail_table;
        end
    end;
    
    generate_game_detail_info = function(game_detail_info)
        game_detail_table = {};
        game_detail_table.privilege = {};
        game_detail_table.developer_info = {};
        game_detail_table.developer_info.relate_game = {};
        game_detail_table.developer_info.relate_game.ids = {};
        game_detail_table.developer_info.relate_game.urls = {};
        game_detail_table.developer_info.relate_game.names = {};
        
        game_detail_table.game_id = game_detail_info.game_id;
        game_detail_table.channel_id = game_library_control.get_bind_channel_id();
        game_detail_table.main_type = game_detail_info.main_type;
        game_detail_table.type = game_detail_info.type;
        game_detail_table.sub_type = game_detail_info.sub_type;
        game_detail_table.distribution_channels = game_detail_info.distribution_channels;
        game_detail_table.name = game_detail_info.name;
        game_detail_table.phase = game_detail_info.phase;
        game_detail_table.operate_phase_class = game_detail_info.operate_phase_class;
        game_detail_table.icon_url = game_detail_info.icon_url;
        game_detail_table.background_url = game_detail_info.background_url;
        game_detail_table.official_url = game_detail_info.official_url;
        game_detail_table.edition = game_detail_info.edition;
        game_detail_table.publisher = game_detail_info.publisher;
        game_detail_table.release_date = game_detail_info.release_date;
                
        game_detail_table.need_activate = game_detail_info.need_activate;
        game_detail_table.installer_size = game_detail_info.installer_size;
        game_detail_table.system_requirement = game_detail_info.system_requirement;
        
        game_detail_table.game_tags = game_detail_info.game_tags;
        game_detail_table.tags = game_detail_info.tags;
        game_detail_table.comments = game_detail_info.comments;
        game_detail_table.show_pre_acts = game_detail_info.show_pre_acts;
        game_detail_table.last_update = game_detail_info.last_update;
        
        game_detail_table.developer_info.name = game_detail_info.developer_info.name;
        game_detail_table.developer_info.description = game_detail_info.developer_info.description;
        game_detail_table.developer_info.game_count = game_detail_info.developer_info.game_count;
        for k = 0, game_detail_table.developer_info.game_count - 1, 1 do
            local g_id = game_detail_info.developer_info.game_ids[k];
            local info = game_library_control.get_game_detail_info(g_id);
            
            game_detail_table.developer_info.relate_game.ids[k] = g_id;
            game_detail_table.developer_info.relate_game.urls[k] = info.icon_url;
            game_detail_table.developer_info.relate_game.names[k] = info.name;
        end
        
        game_detail_table.followed = game_detail_info.followed;
        game_detail_table.new_game = game_detail_info.new_game;
        game_detail_table.installed_game = game_detail_info.installed_game;
        
        game_detail_table.privilege = game_library_control.get_game_privilege(game_detail_info.game_id);
        game_detail_table.privilege_total_info =
            game_library_control.get_game_privilege_total_info(game_detail_info.game_id);
        
        return game_detail_table;
    end;
    
    update_game_detail_info = function()
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local game_detail_info = ierd_tgp.game_library.GameDetailInfo:new_local();
        gl_adapter:get_game_detail_info(game_library_model.current_select_game_id, game_detail_info);
        local game_detail_table = game_library_control.generate_game_detail_info(game_detail_info);
        game_detail_table.op_from = game_library_model.op_from;
        game_detail_table.ad_tag = game_library_model.ad_tag;
        game_detail_table.alpha = game_library_model.alpha;
        game_detail_table.game_state = game_library_control.get_game_sate(game_library_model.current_select_game_id);
        local const_json_str, hold_json_str = base_utility.table_to_json(game_detail_table);
        web_utility.lua_call_js('update_game_detail_info', const_json_str, CID_MAINFRAME_QBWEB);

        base_utility.log(string.format("[game_library_control]update_game_detail_info success, game_id:%d, installer_size:%s",
            tonumber(game_library_model.current_select_game_id), tostring(game_detail_info.installer_size)), true);
        game_library_model.op_from = 0;
        
        -- 单独拉取单个游戏的信息
        if 0 == game_detail_info.installer_size then
            comm_center.call_service("Srv_GetGameRelateInfo", {game_id = game_library_model.current_select_game_id});
        end
    end;

    set_first_open_detail = function(dw_opened)
        local path = "update_tips.firstopendetail.isFirst";
        local path_len = string.len(path);
        -- 读取是否打开过“游戏详情页”
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gl_adapter then
            local val_opened = 0;
            local ret_opened = nil;

            ret_opened = gl_adapter:SetCfgInfoByPathInt(path,
                                                    path_len, dw_opened);
            if nil == ret_opened or false == ret_opened then
                -- 写配置失败，写一次配置,返回没有点击过
                base_utility.log("[set_autoupdate_status] write opened data failed");
            end;
        end;
    end;

    get_first_open_detail = function()

        local b_isFirst_opened = false;
        local path = "update_tips.firstopendetail.isFirst";
        local path_len = string.len(path);
        local  param = {};

        -- 读取是否打开过“游戏详情页”
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gl_adapter then
            local val_opened = 0;
            local ret_opened = nil;
            ret_opened, val_opened = gl_adapter:GetCfgInfoByPathInt(path,
                                                    path_len, val_opened);
            if false == ret_opened then
                -- 读取配置失败，写一次配置,返回没有点击过
                game_library_control.set_first_open_detail(0);
                b_isFirst_opened = true;
                param.isFirst = true;
            else
                -- 读取成功
                if tonumber(val_opened) == 1 then
                    -- 
                    b_isFirst_opened = true;
                    param.isFirst = true;
                    game_library_control.set_first_open_detail(0);
                else
                    -- 打开过
                    b_isFirst_opened = false;
                    param.isFirst = false;
                    -- game_library_control.set_first_open_detail(1);
                end;
            end;
        end;
        local json = base_utility.table_to_json(param);
        web_utility.lua_call_js('get_first_open_detail_js', json, CID_MAINFRAME_QBWEB);
    end;


    update_game_detail_info_ex = function(event_id, json)
        base_utility.log(
            string.format("[game_library_control][update_game_detail_info_ex]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data then
            game_library_model.current_select_game_id = param.data.game_id;
            if param.data.ad_tag ~= nil then
                game_library_model.ad_tag = param.data.ad_tag;
            end
            if param.data.alpha ~= nil then
                game_library_model.alpha = param.data.alpha;
            end
            if param.data.op_from == nil then
                game_library_model.op_from = 0;
            else
                game_library_model.op_from = param.data.op_from;
            end
            game_library_control.update_game_detail_info();
            -- 回调到js
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
                                   
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                local gamelib_event = { game_id = tonumber(param.data.game_id); };
                local gamelib_event_str =  base_utility.table_to_json(gamelib_event);
                game_lib:OnGameLibraryEvent(ierd_tgp.game_library.ON_GAME_DETAIL_OPENING,
                    gamelib_event_str);
            end
        end
    end;

    ShowCustomCoverTips = function(event_id, json)
        base_utility.log(string.format("[game_library_control][ShowCustomCoverTips]json:%s", json));

        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data then
            local tips_param = {
                position = 'center',--参数值: rbottom-右下角，center-居中
                title = param.data.title,
                width = 480,
                height = 230,
                titleIcon = "",
                icon = "",
                tipsIcon = poptips_icons.kSuccess,
                descTitle = "",
                desc = param.data.desc,
                okBtn = {
                    visible = "true",
                    text = param.data.ok_btn_text,
                    close_win = true,
                    handle = function()
                        local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
                        rsp_table.data = {set_cover = true};
                        if param.callback_func ~= nil and param.callback_func ~= "" then
                            local rsp_json = base_utility.table_to_json(rsp_table);
                            web_utility.lua_call_js_ex(
                                param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
                        end
                    end
                },
                cancelBtn = {
                    visible = "true",
                    text = param.data.cancel_btn_text,
                    close_win = true,
                    handle = function()
                        local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
                        rsp_table.data = {set_cover = false};
                        if param.callback_func ~= nil and param.callback_func ~= "" then
                            local rsp_json = base_utility.table_to_json(rsp_table);
                            web_utility.lua_call_js_ex(
                                param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
                        end
                    end
                }
           };

           poptips_control.show_tips(tips_param);
        end
    end;

    update_gamestat_info = function(event_id, json_param)
        web_utility.lua_call_js('update_gamestat_info', json_param, CID_MAINFRAME_QBWEB);
    end;
    
    update_game_state = function(event_id, json_param)
        local const_table_param = base_utility.json_to_table(json_param);
        if (tonumber(game_library_model.current_select_game_id) == const_table_param.game_id) then
        
            local game_state = game_library_control.get_game_sate(const_table_param.game_id);
        
            local gs_json_str = base_utility.table_to_json(game_state);
            if wegame_mainframe_model.web_mainframe_ready == true then
                web_utility.lua_call_js('update_game_state', gs_json_str, CID_MAINFRAME_QBWEB);
            end;

            --广播出去，社交话题房间聊天窗口也需要这个信息
            comm_center.send_broadcast('game_library_broadcast_update_game_state', game_state);
        end

    end;
    
    get_game_sate = function(game_id)
            local gl_adapter =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            local gl_state = ierd_tgp.game_library.GameState:new_local();
            gl_adapter:get_game_state(game_id, gl_state);
            
            local game_state = {};
            game_state.game_id = game_id;
            game_state.state = gl_state.iState;
            game_state.sub_state = gl_state.iSub_state;
            game_state.progress = gl_state.iProgress;
            game_state.size = gl_state.iFile_size;
            game_state.remain_time = gl_state.iRemain_time;
            game_state.speed = gl_state.iDownload_speed;

            return game_state;
    end;
    
    load_game_detail_web = function(game_id, ad_tag, alpha)
        game_library_control.query_gamestat(game_id);
        
        game_library_model.current_select_game_id = game_id;
        game_library_model.alpha = alpha;
        
        -- use TGP.jump in frame instead
        -- local table_str = {};
        -- table_str.type = "open_game_library";
        -- table_str.sub_page = "GAME_DETAIL";
        -- table_str.game_id = game_id;
        -- table_str.ad_tag = ad_tag;
        -- table_str.alpha = alpha;
        -- local json_str = base_utility.table_to_json(table_str);
        -- web_utility.open_link(json_str);
    end;
    
    load_game_list_web = function()
        game_library_model.current_select_game_id = 0;
        local table_str = {};
        table_str.type = "mmo_game";
        table_str.sub_page = "ALL_GAME";
        table_str.category = game_library_model.category;
        table_str.sort = game_library_model.sort;
        local json_str = base_utility.table_to_json(table_str);
        web_utility.open_link(json_str);
    end;
    
    query_game_library_common_info = function()
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:query_common_info();
        base_utility.log("[game_library_control]query_game_library_common_info.");
    end;
    
    query_gamestat = function(game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        -- gl_adapter:query_gamestat(game_id);
        base_utility.log("[game_library_control]query_gamestat.");
    end;
    
    query_game_attent_list = function()
        game_library_model.game_attent_query_id =
            game_library_model.ATTENT_GAME_LIST_MMOG;
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:query_new_game_attent_list();
        base_utility.log("[game_library_control]query_new_game_attent_list.");
    end;
    
    game_follow_request = function(game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:follow_game(game_id);
        base_utility.log("[game_library_control]game_follow_request.");

        game_library_control.update_attent_game_list(game_id);
    end;

    update_attent_game_list = function(game_id)
        base_utility.log("[update_attent_game_list] follow game,id: "..game_id);
        if game_library_model.attent_game_list == nil then
            game_library_model.attent_game_list = {};
        end

        if game_library_model.attent_game_list.game_list == nil then
            game_library_model.attent_game_list.game_list = {};
        end

        if game_library_model.attent_game_list.game_num == nil then
            game_library_model.attent_game_list.game_num = 0;
        end

        local found = false;
        local i = 0;
        for i = 1, game_library_model.attent_game_list.game_num, 1 do
            if game_id == game_library_model.attent_game_list.game_list[i].game_id then
                found = true;
                break;
            end;
        end;
        if not found then
            base_utility.log("not found add id");
            local number = game_library_model.attent_game_list.game_num + 1;
            game_library_model.attent_game_list.game_num = number;
            local game_info = {};
            game_info.game_id = game_id;
            game_library_model.attent_game_list.game_list[number] = game_info;
        end
    end;
    
    download_game = function(dl_type, game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if dl_type == true then
            local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = game_id});
            if data_out ~= nil and not data_out.bAllowContinue then
                return
            end
            local channel = gl_adapter:get_game_distribution_channels(game_id);
            if channel == 2 then
                local json_str = base_utility.table_to_json({ game_id = game_id });
                event_center.send_event(branch_selector_event.OPEN_GAME_BRANCH_SELECTOR, json_str);
            else 
                gl_adapter:download_game(game_id);
            end
        else
            gl_adapter:pause_download_game(game_id);
        end
    end;
    
    download_cancel = function(game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:cancel_download_game(game_id);
    end;
    
    download_continue = function(game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:continue_download_game(game_id);
    end;
    
    add_game = function(game_id)
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:add_game(game_id);
    end;
    
    translate_catgory = function(sub_page)
        if sub_page == "ALL_GAME" then
            return 0;
        end
        return 0;
    end;
    
    launch_web_game = function(game_id, ad_param)
        local table_str = {};
        table_str.type = "launch_webgame";
        table_str.game_id = game_id;
        table_str.op_from = 1;
        table_str.ad_param = ad_param;
        table_str.zone_type = ierd_tgp.game_library.ZONE_TYPE_FORMAL;

        table_str.ver = "";
        local json_str = base_utility.table_to_json(table_str);
        web_utility.open_link(json_str);
    end;
    
    on_js_callback = function(event_id, json)
        local param = base_utility.json_to_table(json);
        if param.load_flag == 1 then
            game_library_control.update_game_privilege();
        end
        
        if param.load_flag == 2 then
            game_library_control.update_game_detail_info();
        end
        
        if param.load_flag == 3 then
            local info = {};
            info.vid = param.vid;
            info.qblink = param.qblink;
            event_center.send_event(browser_event.PLAY_VIDEO, base_utility.table_to_json(info));
        end
        
        if param.type == 0 then
            if param.mmog ~= nil and param.mmog == 1 then
                game_library_model.mmog_page = true;
            end
            game_library_control.update_game_list(param.main_type, param.cate, param.sub_type, param.sort);
        end
        
        if param.type == 2 then
            local ad_tag = "";
            if param.ad_tag ~= nil and param.alpha ~= nil then
                ad_tag = param.ad_tag;
                game_library_control.load_game_detail_web(param.game_id, ad_tag, param.alpha);
            end;
        end
        
        if param.type == 3 then
            game_library_control.load_game_list_web();
        end
        
        if param.type == 4 then
            game_library_control.game_follow_request(param.game_id);
        end
        
        if param.type == 5 then
            game_library_control.download_game(true, param.game_id);
        end
        
        if param.type == 6 then
            game_library_control.download_game(false, param.game_id);
        end
        
        if param.type == 7 then
            game_library_control.download_cancel(param.game_id);
        end
        
        if param.type == 8 then
            game_library_control.download_continue(param.game_id);
        end
        
        if param.type == 9 then
            game_library_control.add_game(param.game_id);
        end
        
        if param.type == 10 then
            game_library_control.launch_web_game(param.game_id, param.ad_param);
        end

        -- 下载设置
        if param.type == 11 then
            base_utility.log("[on_js_callback]下载设置，显示下载设置窗口");
            local str = {tab_id = 1};
            event_center.send_event(main_menue_event.MAIN_MENUE_SETTING, base_utility.table_to_json(str));
        end
        
        -- 获取符合类型的所有游戏信息
        if param.type == 13 then
            base_utility.log("[on_js_callback]get all game list info");
            game_library_control.get_all_game_list_info(param.main_type, param.cate, param.sub_type, param.sort);
        end

        base_utility.log("[game_library_control]on_js_callback "..json);
    end;

    -- 更新个人的信息的等级
    set_info = function (event_id, json_str)
        if json_str == "" then
            local user_game_info = ierd_tgp.users_info.User_game_info:new();
            local users_info_adapter =
                base_utility.get_component_adapter("IUsers_info_module",
                    "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
            game_library_model.current_level = user_game_info.user_level;
            user_game_info:delete();
        else
            if json_str == nil or type(json_str) == "number" then
                return;
            end;
            local user_game_info = base_utility.json_to_table(json_str);
            if user_game_info ~= nil and type(user_game_info) ~= "number" then
                game_library_model.current_level = user_game_info.user_level;
            end;
        end;
    end;

    get_user_level = function (event_id, json)
        -- body
        local data = {};
        data["current_level"] = game_library_model.current_level;
        local json_str = base_utility.table_to_json(data);
        --调用js脚本，吐回数据
        web_utility.lua_call_js('on_request_get_current_level', json_str, CID_TGPBROWSER_BROWSER);
        base_utility.log("[game_library_control get_user_level]on_request_get_current_level success.");
    end;

    send_game_activate_result = function(event_id, json)
        -- body
        -- 通知C++端
        local data = base_utility.json_to_table(json);
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gl_adapter:SetActiveGameResult(data.game_id, data.result);
        base_utility.log("call SetActiveGameResult"..json)
        -- 通知js端
        if data ~= nil and data.type == 'game_activate_result' then
            local json_str = {};
            json_str["result"] = data.result;
            json_str["game_id"] = data.game_id;
            json_str["user_cancel"] = data.user_cancel;
            web_utility.lua_call_js('on_game_activate_notify', base_utility.table_to_json(json_str), CID_MAINFRAME_QBWEB);
        end;
        base_utility.log("send_game_activate_result"..json);
    end;
    
    handle_get_game_states = function(event_id, json)
        base_utility.log("call handle_get_game_states, json: " .. json)
       
        local game_states = banner_model.get_game_state_list();
        local param = {};
            
        for i = 0, (game_states.count - 1) do
            param[tostring(game_states.game_state[i].iGame_id)] =
                game_states.game_state[i].iState;
        end;
            
        local json_str = base_utility.table_to_json(param);
        base_utility.log("update_game_states" .. json_str);
            
        local data = base_utility.json_to_table(json);
        local wnd_id = data.wnd_id;
        if wnd_id == nil or wnd_id == 0 then
            wnd_id = CID_MAINFRAME_QBWEB;
        end;
        web_utility.lua_call_js_by_id('get_game_states', json_str, wnd_id); 
        
    end;

    -- 处理激活界面查询游戏是否已经安装
    request_game_install = function (event_id, json)
        -- body
        local data = base_utility.json_to_table(json);
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local  temp_list = ierd_tgp.game_library.GameCategory:new();
        if game_lib ~= nil then
            game_lib:get_game_category(0, tool_box_model.game_category_type.MY_GAMES, 0, 
            tool_box_model.game_category_sort_type.BY_DATE, temp_list);
        end;
        local count = temp_list.game_count;
        local find_data = 0;
        local  gameid = tonumber(data.game_id);
        for n = 1, count do
            if temp_list.game_list[n - 1] == gameid then 
                find_data = 1
            end;
        end;
        local json_str = {};
        json_str["state"] = find_data;
        web_utility.lua_call_js('on_request_get_game_install_state', base_utility.table_to_json(json_str), CID_TGPBROWSER_BROWSER);
        temp_list:delete();
    end;
    
    -- brief: 解析cmd，执行open_link
    -- 由于C++端使用json::fastwriter函数生成的param会带有换行符号
    -- 会导致open_link会执行第一个分支，转换命令
    -- 这里先要去除换行符号
    cpp_open_link = function(cmd, param)
        local pos = string.find(param, "\n")
        if pos ~= nil then
            local  json = string.sub(param, 1, pos-1)
            web_utility.open_link(json);
        end

        if pos == nil then
            web_utility.open_link(param);
        end

        base_utility.log("[game_library_control]PLUGINMGR_ON_TGP_OPEN_LINK"..cmd)
    end;
    
    -- 重新实现了OPEN_LINK游戏库的跳转
    open_link = function(event_id, json)
        local json_table = base_utility.json_to_table(json);
        if json_table == nil then
            return;
        end;
        local game_id = nil;
        local game_info = nil;
        local game_type = nil;
        local game_sub_type = nil;
        local game_main_type = nil;
        if json_table.type == "mmo_game" then
            --  rail game的游戏详情页跟其他游戏分开了
            if json_table.game_id ~= nil then
                if type(json_table.game_id) ~= "number" then
                    game_id = tonumber(json_table.game_id);
                else
                    game_id = json_table.game_id;
                end
            end
            if game_id ~= nil then
                game_info = banner_model.get_game_base_info(game_id);
            end
            if game_info ~= nil then
                game_main_type = game_info.main_type;
                game_type = game_info.type;
                game_sub_type = game_info.sub_type;
            end
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_main_type ~= nil and game_main_type == game_library_model.game_main_type.CONSOLE_GAME then
                game_library_control.jump_to_rail_game_detail(json_table);
                return;
            end;
            
            local module_url = nil;
            local id_and_url = game_library_control.sub_page2id_url_table[json_table.sub_page];
            if id_and_url == nil then
                id_and_url = game_library_control.sub_page2id_url_table.ALL_GAME;
                module_url = id_and_url.url;
            else
                module_url = id_and_url.url;
                if json_table.sub_page == "GAME_DETAIL" then
                    -- jimzpxu: 增加大型网游详情页独立处理
                    if json_table.game_id ~= nil then
                        if game_main_type ~= nil and game_main_type == game_library_model.game_main_type.MMOG_GAME then
                            module_url = game_library_control.sub_page2id_url_table.GAME_DETAIL.url;
                        end
                    end
                end
            end;
            local params = {};
            params.module_id = id_and_url.id;
            if json_table.game_id == nil then
                params.module_url = module_url;
            else
                params.module_url = module_url.."&game_id="..json_table.game_id;
            end
            if json_table.page_tab ~= nil and json_table.page_tab ~= "" then 
                params.module_url = params.module_url .. "&page_tab=" .. json_table.page_tab;
            end
            if json_table.ctype ~= nil and json_table.ctype ~= "" then 
                params.module_url = params.module_url .. "&ctype=" .. json_table.ctype;
            end
            if json_table.from ~= nil and json_table.from ~= "" then 
                params.module_url = params.module_url .. "&from=" .. json_table.from;
            end
            event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(params));
            
            -- if json_table.sub_page == "GAME_DETAIL" then
            --     -- 这里是因为别的地方没有框架方法，这里代为处理
            --     game_library_control.load_game_detail_web(json_table.game_id, "", "");
            --     game_library_control.update_game_detail_info();
            -- end;
            
        elseif json_table.type == "homepage" then
            local id_and_url = game_library_control.sub_page2id_url_table.HOME_PAGE;
            local params = {};
            params.module_id = id_and_url.id;
            params.module_url = id_and_url.url;
            event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(params));
        elseif json_table.type == "download" then
            if nil ~= json_table.game_id then
                local game_id = tonumber(json_table.game_id);
                game_library_control.download_mmog_game(game_id);
            end
        end;
    end;
        
    jump_to_rail_game_detail = function(json_table)
        local game_id = json_table.game_id;
        if game_id == nil then
            return;
        end;
        local param = {};
        param.type = "jump_url";
        -- param.sub_page = "GAME_DETAIL";
        param.game_id = game_id;
        param.url = LoadStr("WEGAME_DETAIL_URL")..game_id;
        -- 确保刷新页面
        param.url = param.url .. "&refresh=" .. tostring(math.random(65536));
        if json_table.page_tab ~= nil and json_table.page_tab ~= "" then 
            param.url = param.url .. "&page_tab=" .. json_table.page_tab;
        end
        if json_table.ctype ~= nil and json_table.ctype ~= "" then 
            param.url = param.url .. "&ctype=" .. json_table.ctype;
        end
        if json_table.from ~= nil and json_table.from ~= "" then 
            param.url = param.url .. "&from=" .. json_table.from;
        end
        param.op_from = 0;
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;

    -- 原来的extendboard中打开游戏库中的上报放在这里进行
    report_open_game_library_qos = function(param)
        local qos_data = ierd_tgp.tpf_ui.LuaQosData:new();
        qos_data.oc_type = 1;   --即时上报
        qos_data.qos_report_id = 176; 
        qos_data.int_count = param.int_count;
        qos_data.str_count = param.str_count;
        for i = 1, param.int_count do
            qos_data.int_val[i - 1] = param.int_val[i];
        end;
        --字符串数据的上报暂时不需要，以后根据需要添加
        base_utility.lua_qos(qos_data);
        qos_data:delete();
    end;

    -- 显示下载设置窗口
    show_download_setting_wnd = function (game_id)
        base_utility.log(string.format(
            "[game_library_control][show_download_setting_wnd]game_id:%d", game_id));
        local gl_adapter =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gl_adapter then
            gl_adapter:ShowDownloadSettingWnd(game_id);
        end
    end;

    -- 查询玩过的游戏列表
    query_play_game_list = function(event_id, json)
        base_utility.log(string.format("[query_play_game_list]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data  and
           param.callback_func ~= nil and param.callback_func ~= "" then
            local rsp_info = {};
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_info.rsp_table = rsp_table;
            rsp_info.callback_func = param.callback_func;
            local data_src = 1;
            if param.data.data_src ~= nil then
                data_src = tonumber(param.data.data_src);
            end
            local gl_adapter =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if gl_adapter ~= nil then
                game_library_control.push_query_play_game_rsp_info(rsp_info);
                gl_adapter:QueryPlayGameInfoList(data_src);
                base_utility.log("[game_library_control]QueryPlayGameInfoList.");
            else
                base_utility.log("[game_library_control]gl_adapter is nil.");
            end
        end
    end;

    query_play_game_list_rsp = function(event_id, json_param)
        base_utility.log("query_play_game_list_rsp called");
        if nil ~= json_param then
            local rsp_info = game_library_control.pop_query_play_game_rsp_info();
            if rsp_info ~= nil and 
               rsp_info.callback_func ~= nil  and
               rsp_info.rsp_table ~= nil then
                rsp_info.rsp_table.data = base_utility.json_to_table(json_param);
                local i = 0;
                local param = {};
                param.data = {};
                param.data.game_id_list = {};
                param.data.game_id_count = rsp_info.rsp_table.data.game_num;
                for i = 1, rsp_info.rsp_table.data.game_num, 1 do
                    param.data.game_id_list[i] = rsp_info.rsp_table.data.game_list[i].game_id;
                end
                local temp_list = game_library_control.get_game_info_by_id(param);
                for i = 1, rsp_info.rsp_table.data.game_num, 1 do
                    for index = 1, #temp_list do
                        if temp_list[index].game_id ==  rsp_info.rsp_table.data.game_list[i].game_id then
                            rsp_info.rsp_table.data.game_list[i].game_name = temp_list[index].game_name;
                            break;
                        end
                    end
                    if rsp_info.rsp_table.data.game_list[i].game_name == nil then
                        rsp_info.rsp_table.data.game_list[i].game_name = "";
                    end
                end
                local rsp_json = base_utility.table_to_json(rsp_info.rsp_table);
                web_utility.lua_call_js_ex(rsp_info.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    -- 查询关注的游戏列表
    query_attent_game_list = function(event_id, json)
        base_utility.log(string.format("[query_play_game_list]json:%s", json));
        game_library_model.game_attent_query_id =
            game_library_model.ATTENT_GAME_LIST_BOUTIQUE;
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data  and
            param.callback_func ~= nil and param.callback_func ~= "" then
            local rsp_info = {};
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_info.rsp_table = rsp_table;
            rsp_info.callback_func = param.callback_func;
            game_library_control.push_query_attent_game_rsp_info(rsp_info);
            game_library_control.query_attent_game_list_rsp();
        end
    end;

    push_query_attent_game_rsp_info = function(rsp)
        local rsp_json = base_utility.table_to_json(rsp);
        base_utility.log("push_query_attent_game_rsp_info, rsp: "..rsp_json);
        if rsp == nil then
            base_utility.log("push_query_attent_game_rsp_info, rsp is error!");
            return;
        end
        local last = game_library_model.attent_game_rsp_info_queue.last + 1;
        game_library_model.attent_game_rsp_info_queue.last = last;
        game_library_model.attent_game_rsp_info_queue[last] = rsp;
    end;

    push_query_play_game_rsp_info = function(rsp)
        local rsp_json = base_utility.table_to_json(rsp);
        base_utility.log("push_query_play_game_rsp_info, rsp: "..rsp_json);
        if rsp == nil then
            base_utility.log("push_query_play_game_rsp_info, rsp is error!");
            return;
        end
        game_library_model.play_game_rsp_info_queue = rsp;
    end;

    pop_query_attent_game_rsp_info = function()
        local first = game_library_model.attent_game_rsp_info_queue.first;
        if first > game_library_model.attent_game_rsp_info_queue.last then 
            base_utility.log("game_library_model.attent_game_rsp_info_queue empty!");
            return nil;
        end
        local rsp = game_library_model.attent_game_rsp_info_queue[first];
        game_library_model.attent_game_rsp_info_queue[first] = nil;
        game_library_model.attent_game_rsp_info_queue.first = game_library_model.attent_game_rsp_info_queue.first + 1;
        return rsp;
    end;

    pop_query_play_game_rsp_info = function()
        local rsp = game_library_model.play_game_rsp_info_queue;
        return rsp;
    end;
    
    -- 获取距离现在最近已上线的n个游戏
    query_nearest_game_list = function(event_id, json_param)
        base_utility.log("query_nearest_game_list called");
        if nil ~= json_param then
            local param = base_utility.json_to_table(json_param);
            local game_list, total = game_library_control.get_nearest_release_game();
            local result = {};

            local cur_date = os.date("%Y%m%d");
            local nearest_game_list = {};
            local i = 0;
            local j = 0;
            for i = 1, total, 1 do
                if j >= param.data.number then
                    break;
                end;
                if cur_date >=  game_list[i].release_date then
                    j = j + 1;
                    nearest_game_list[j] = game_list[i];
                end;
            end;
            
            result.number = j;
            result.game_list = nearest_game_list;
            local js = base_utility.table_to_json(result);
            base_utility.log("query_nearest_game_list_rsp "..js);
            web_utility.lua_call_js_ex("query_nearest_game_list_rsp", js, CID_MAINFRAME_QBWEB);
        end
        
    end;

    -- 按发布时间排序的游戏列表
    get_nearest_release_game = function()
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end
        nearest_release_game = {};
        local i = 0;
        local j = 0;
        local all_game_info = banner_model.game_base_info_list;
        for i = 0, all_game_info.count - 1, 1 do
            if (all_game_info.game_base_info[i].type == game_library_model.ROLE_GAME_TYPE or
                all_game_info.game_base_info[i].type == game_library_model.MATCH_GAME_TYPE or
                all_game_info.game_base_info[i].type == game_library_model.LEISURE_GAME_TYPE) and 
               (all_game_info.game_base_info[i].release_date ~= "") then
                local game_info = {};
                game_info.game_id = all_game_info.game_base_info[i].id;
                game_info.game_icon = all_game_info.game_base_info[i].poster_url;
                game_info.game_name = all_game_info.game_base_info[i].name;
                game_info.slogan = all_game_info.game_base_info[i].slogan;
                game_info.type = all_game_info.game_base_info[i].type;
                game_info.release_date = all_game_info.game_base_info[i].release_date;
                base_utility.log("get_nearest_release_game  release_time:"..all_game_info.game_base_info[i].release_date);
                j = j+1;
                nearest_release_game[j] = game_info;
            end;
        end;
        
        base_utility.log("get_nearest_release_game  size:"..j);
        if j > 1 then
            table.sort(nearest_release_game, 
                function(a, b)
                    return a.release_date > b.release_date;
                end
             );
        end
        return nearest_release_game, j;
    end;

    -- 查询新游推荐的游戏名称、图片
    query_rec_game_name = function (event_id, json_param)
        base_utility.log("query_rec_game_name called");
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end

        if nil ~= json_param then
            local param = base_utility.json_to_table(json_param);
            local number = param.data.number;
            local result = {};
            result.game_info = {};
            local i = 0;
            for i = 1, number, 1 do
                local game_info = {};
                game_info.game_id = param.data.rec_game_id[i];

                local game_base_info = banner_model.get_game_base_info(tonumber(game_info.game_id));
                if game_base_info ~= nil then
                    game_info.game_name = game_base_info.name;
                    game_info.game_url = game_base_info.poster_url;
                end
                result.game_info[i] = game_info;
            end
            local js = base_utility.table_to_json(result);
            base_utility.log("query_rec_game_name called"..js);
            web_utility.lua_call_js_ex("query_rec_game_name_rsp", js, CID_MAINFRAME_QBWEB);
         end;
    end;

    -- 已关注的游戏列表
    query_game_attent_list_rsp = function(event_id, json_param)
        base_utility.log("query_game_attent_list_rsp called");
        if nil ~= json_param then
            local tem_list = base_utility.json_to_table(json_param);
            game_library_model.attent_game_list = {};
            game_library_model.attent_game_list.game_num = tem_list.game_num;
            game_library_model.attent_game_list.game_list = {};
            local i = 0;
            for i = 1, tem_list.game_num, 1 do
                 local game_info = {};
                 game_info.game_id = tem_list.game_list[i].game_id
                 game_library_model.attent_game_list.game_list[i] = game_info;
            end
        end;
    end;

    -- web获取关注的游戏列表
    query_attent_game_list_rsp = function()
        game_library_control.update_expect_game_list();
        if game_library_model.attent_game_list ~= nil and
            game_library_model.attent_game_list.game_list ~= nil then
            local tem_list = game_library_model.attent_game_list.game_list;
            local i = 0;
            local game_info = {};
            game_info.data = {};
            local game_id_list = {};
            for i = 1, game_library_model.attent_game_list.game_num, 1 do
                 game_id_list[i] = tem_list[i].game_id
            end

            game_info.data.game_id_list = game_id_list;
            game_info.data.game_id_count = game_library_model.attent_game_list.game_num;

            local rsp_info = game_library_control.pop_query_attent_game_rsp_info();
            if rsp_info ~= nil and 
                rsp_info.callback_func ~= nil  and
                rsp_info.rsp_table ~= nil then
                    rsp_info.rsp_table.data = game_library_control.get_game_info_by_id(game_info);
                    local rsp_json = base_utility.table_to_json(rsp_info.rsp_table);
                    web_utility.lua_call_js_ex(rsp_info.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    -- 更新期待游戏的关注状态
    update_expect_game_list = function()
        if game_library_model.expect_game_list == nil then
            game_library_model.expect_game_list = {};
            game_library_model.expect_game_list.game_list = {};
            game_library_model.expect_game_list.count = 0;
            game_library_model.expect_game_list.game_list, game_library_model.expect_game_list.count = 
             game_library_control.get_new_game_list();
        end
        local i = 0;
        for i = 1, game_library_model.expect_game_list.count, 1 do
            local game_id = game_library_model.expect_game_list.game_list[i].game_id;
            local j = 0;
            for j = 1, game_library_model.attent_game_list.game_num, 1 do
                if game_id ==  game_library_model.attent_game_list.game_list[j].game_id then
                    game_library_model.expect_game_list.game_list[i].attented = 1;
                    base_utility.log("found attented game "..game_id);
                    break;
                end
            end
        end
    end;

    query_expect_game_rank = function(event_id, json_param)
        game_library_control.update_expect_game_list();
        if game_library_model.expect_game_list ~= nil then
            local js = base_utility.table_to_json(game_library_model.expect_game_list);
            base_utility.log("query_expect_game_rank_rsp called"..js);
            web_utility.lua_call_js_ex("query_expect_game_rank_rsp", js, CID_MAINFRAME_QBWEB);
        end;
    end;

    -- 获取新游列表
    get_new_game_list = function()
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end
        new_game_list = {};
        local i = 0;
        local j = 0;
        local all_game_info = banner_model.game_base_info_list;
        for i = 0, all_game_info.count - 1, 1 do
            if (all_game_info.game_base_info[i].type == game_library_model.ROLE_GAME_TYPE or
                all_game_info.game_base_info[i].type == game_library_model.MATCH_GAME_TYPE or
                all_game_info.game_base_info[i].type == game_library_model.LEISURE_GAME_TYPE) and 
               (all_game_info.game_base_info[i].is_new) then
                local game_info = {};
                game_info.game_id = all_game_info.game_base_info[i].id;
                game_info.game_icon = all_game_info.game_base_info[i].poster_url;
                game_info.game_name = all_game_info.game_base_info[i].name;
                game_info.game_new = all_game_info.game_base_info[i].is_new;
                game_info.type = all_game_info.game_base_info[i].type;
                game_info.release_date = all_game_info.game_base_info[i].release_date;
                base_utility.log("get_new_game_list  release_time:"..all_game_info.game_base_info[i].release_date);
                game_info.attented = 0;

                j = j+1;
                new_game_list[j] = game_info;
            end;
        end;
        
        base_utility.log("get_new_game_list  size:"..j);
        return new_game_list, j;
    end;

    -- 透传所有符合条件的游戏信息到页面
    get_all_game_list_info = function(main_type, category, sub_type, sort)
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end
        local game_list, hold = game_library_control.get_game_list(main_type, category, sub_type, sort);
        local game_list_info = game_library_control.generate_game_info(game_list);
        local const_json_str, hold_json_str = base_utility.table_to_json(game_list_info);

        web_utility.lua_call_js('get_all_game_list_info', const_json_str, CID_MAINFRAME_QBWEB);
        base_utility.log("[game_library_control]get_all_game_list_info success.");
    end;

    -- 游戏启动更新前会回调到这里
    before_update_game = function(event_id, json)
        -- 暂时没有什么可干的, 代码框架先留着
    end;

    -- 根据游戏id查询游戏的信息
    get_game_info_by_id = function(param)
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end

        if nil ~= param and nil ~= param.data  and 
           nil ~= param.data.game_id_list and nil ~= param.data.game_id_count then
            local game_info_list = {};
            local i = 1;
            local count = 0;
            for i = 1, tonumber(param.data.game_id_count), 1 do
                local game_info = {};
                if param.data.game_id_list[i] ~= "" then
                    local game_id = tonumber(param.data.game_id_list[i]);
                    local game_base_info = banner_model.get_game_base_info(game_id);
                    if nil ~= game_base_info then
                        count = count + 1;
                        game_info.game_id = game_id;
                        game_info.game_icon = game_base_info.poster_url;
                        game_info.game_name = game_base_info.name;
                        game_info.game_main_type = game_base_info.main_type;
                        game_info.game_type = game_base_info.type;
                        game_info.game_sub_type = game_base_info.sub_type;
                        game_info.game_desc = game_base_info.slogan;
                        game_info.game_new = game_base_info.is_new;
                        game_info.alpha = game_base_info.name_first_letter;
                        game_info.name_type = game_base_info.name_type;
                        game_info.game_installed = game_base_info.is_installed;
                        game_info.game_tags = game_base_info.game_tags;
                        game_info.tags = game_base_info.tags;
                        game_info.comments = game_base_info.comments;
                        game_info.icon_url = game_base_info.icon_url;
                        game_info.release_date = game_base_info.release_date;
                        game_info.game_hidden = game_base_info.is_hidden;
                        
                        local detail_info = game_library_control.get_game_detail_info(game_id);
                        if nil ~= detail_info then
                            game_info.support_fix = detail_info.support_fix;
                            game_info.game_install_state = detail_info.game_install_state;
                            game_info.publish = detail_info.publish;
                            game_info.poster_url_v = detail_info.poster_url_v;
                            game_info.installer_size = detail_info.installer_size;
                        end
                        game_info_list[count] = game_info;
                    end
                end
            end

            return game_info_list;
        end
    end;
    --
    query_game_info_by_id = function(event_id, json)
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end

        base_utility.log(string.format("[query_game_info_by_id]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data  and 
           nil ~= param.data.game_id_list and nil ~= param.data.game_id_count and
           nil ~= param.callback_func and param.callback_func ~= "" then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");

            rsp_table.data = game_library_control.get_game_info_by_id(param);

            local rsp_json = base_utility.table_to_json(rsp_table);
            web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
        end
    end;

    query_game_info_by_id_v2 = function(event_id, json)
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end

        base_utility.log(string.format("[query_game_info_by_id_v2]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data  and 
           nil ~= param.data.game_id_list and nil ~= param.data.game_id_count and
           nil ~= param.callback_func and param.callback_func ~= "" then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_table.data = game_library_control.get_game_info_by_id(param);
            local rsp_json = base_utility.table_to_json(rsp_table);
            local custom_id = CID_MAINFRAME_QBWEB;
            if nil ~= param.custom_id and type(param.custom_id) == 'number' then
                base_utility.log(string.format("[query_game_info_by_id_v2]cid:%d", param.custom_id));
                custom_id = param.custom_id;
            end
            web_utility.lua_call_js_ex(param.callback_func, rsp_json, custom_id);
        end
    end;

    on_query_game_info = function(param)
        if nil == param or type(param) ~= 'table' then return end;
        if game_library_model.game_base_info_ready == false then
            game_library_control.init_game_base_info();
        end
        base_utility.log(string.format("[on_query_game_info]json:%s",
                         base_utility.table_to_json(param)));
        local result = {};
        if nil ~= param.game_id_list and nil ~= param.game_id_count then
            local data = {
                data = {
                    game_id_count = param.game_id_count,
                    game_id_list = param.game_id_list,
                }
            };
            result = game_library_control.get_game_info_by_id(data);
        end
        return result;
    end;

    add_assist_silently = function(event_id, json)
        local param = base_utility.json_to_table(json);
        local game_id = param.status;
        if game_id == nil or game_id == 0 then
            return;
        end;

        local evt_param_str = '{"data":{"game_id":' .. game_id ..'}}';
        event_center.send_event(extendboard_event.ADD_MOBILE_GAME_ASSISTANT, evt_param_str);
    end;

    --[[ lcu remove
    update_lcu_state = function(event_id, json)
        web_utility.lua_call_js_ex("tgp_update_lcu_state", json, CID_MAINFRAME_QBWEB);
    end;
    ]]

    -- 统一处理JS的回调
    handle_js_callback = function(event_id, json_param)
        local param = base_utility.json_to_table(json_param);
        local func_name = param.func_name;
        local json = param.json;
        ierd_tgp.tpf_ui.Log("[game_library]handle_js_callback.func_name="..func_name);
        if func_name == "jc_batch_get_game_state_info" then
            game_library_control.batch_get_game_state_info(json);
        end
    end;

    --根据游戏id列表，批量获取游戏状态信息
    batch_get_game_state_info = function(json_param)
        if json_param == nil then return end
        local param_input = base_utility.json_to_table(json_param);
        if param_input ~= nil and param_input.data ~= nil and 
            param_input.data.game_id_list ~= nil and type(param_input.data.game_id_list) == 'table' then
            local rsp_table = web_utility.web_rsp_comm_data(param_input, 0, "");
            rsp_table.data = {};
            local my_game_list = ierd_tgp.game_library.Game_list:new_local();
            local visible_game_count = 0;
            local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                game_lib:get_visible_games(my_game_list);
                if banner_model.is_netbar == 1 then
                    visible_game_count = game_lib:GetNetbarVisibleGameCount();
                else
                    visible_game_count = my_game_list.count;
                end
            end
            local game_id_list = {};
            for i, game_id in ipairs(param_input.data.game_id_list) do
                if game_id ~= nil and game_id ~= 0 then
                    local game_state = banner_model.get_game_state(game_id);
                    if game_state ~= nil then
                        local game_state_info = {
                            game_id = game_id,
                            status = game_state.iState,
                            sub_status = game_state.iSub_state,
                            added_game = game_library_control.is_my_game_by_id(my_game_list, visible_game_count, game_id);
                        };
                        table.insert(rsp_table.data, game_state_info);
                        table.insert(game_id_list, game_id);
                    end
                end
            end

            if game_lib ~= nil then
                local game_id_list_json = base_utility.table_to_json(game_id_list);
                game_lib:QueryGameListFullURL(game_id_list_json);
            end

            if param_input.callback_func ~= nil and param_input.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param_input.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    is_my_game_by_id = function(my_game_list, visible_game_count, game_id_param)
        local is_added = 0;
        local loop_count = my_game_list.count;
        if visible_game_count < my_game_list.count then
            loop_count = visible_game_count;
        end
       
        for i = 0, loop_count - 1 do
            if my_game_list.game_id_list[i] == game_id_param then
                is_added = 1;
                break;
            end
        end
        return is_added;
    end;

    on_get_mmog_game_release_status = function (event_id, json_param)
        if nil ~= json_param then
            base_utility.log("[game_library_control]query_mmog_whitelist_rsp called"..json_param);
            local json_data = base_utility.json_to_table(json_param);
            if nil ~= json_data then
                local tast_check = json_data["task_check"];
                local is_release = json_data["is_release"];
                local game_id = json_data["game_id"];
                if nil ~= tast_check and 
                    "game_library_control.on_get_mmog_game_release_status" == tast_check then
                    if nil ~= is_release and nil ~= game_id then
                        if true == is_release then
                            game_library_control.download_mmog_game(tonumber(game_id));
                        else
                            -- 未在白名单中 跳转详情页
                            local detail_page_url = 
                                game_library_control.sub_page2id_url_table.GAME_DETAIL.url.."&game_id=";
                            local param = {};
                            param.type = "jump_url";
                            param.sub_page = "GAME_DETAIL";
                            param.game_id = game_id;
                            param.url = detail_page_url .. game_id;
                            param.op_from = 0;
                            web_utility.open_link(base_utility.table_to_json(param));
                        end
                    end
                end
            end
        end
    end;
    
    on_push_gift_info = function (event_id, json_param)
        base_utility.log(
            "[game_library_control]on_push_gift_info called, json_param: " .. json_param);
            
        local callback_func = web_register_center.get_func_by_browser(
            "cj_push_obtained_gift_info", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = web_utility.notify_event_comm_data(
                "cj_push_obtained_gift_info", nil, nil, GAME_ID.GAME_ID_TGP);
            local param_table = base_utility.json_to_table(json_param);
            if param_table ~= nil then
                rsp_data_table.data.gift_info = param_table;
                web_utility.lua_call_js_ex(callback_func, 
                        base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
            end;
        end
    end;

    download_mmog_game = function (game_id)
        local action_info = { game_id_ = game_id };
        base_utility.log("[game_library_control][download_mmog_game] action_info is : "
            .. base_utility.table_to_json(action_info));
        WGGameAction.download(action_info);
    end;

    open_detail_page_or_start_download = function (data)
        if data == nil or data.game_id == nil then
            base_utility.log("[game_library_control]open_detail_page_or_start_download: param data is nil");
            return;
        end
        local game_id = data.game_id;
        local game_info = banner_model.get_game_base_info(game_id);
        if game_info ~= nil then
            if game_info.is_installed == false then
                base_utility.log(
                "[game_library_control]open_detail_page_or_start_download: start download game. game_id=" .. game_id);
                local data = {};
                data.game_id = game_id;
                data.extra_info = {need_broadcast = true, from = "open_detail_page_or_start_download"};
                web_utility.open_game_detail_page(data);
            else
                base_utility.log(
                "[game_library_control]open_detail_page_or_start_download: open game detail page. game_id=" .. game_id);
                web_utility.open_game_detail_page({game_id=game_id});
            end;
        else
            base_utility.log(
            "[game_library_control]open_detail_page_or_start_download: get game_install_state failed, open game detail page. game_id=" .. game_id);
            web_utility.open_game_detail_page({game_id = game_id});
        end;
    end;

    handle_auto_download = function (data)
        if data == nil then
            base_utility.log("[game_library_control]handle_auto_download: param_data is nil");
            return;
        end;
        if data.game_id ~= nil and 
            data.extra_info ~= nil and data.extra_info.from ~= nil
            and data.extra_info.from == "open_detail_page_or_start_download" then 
            local game_id = 0;
            if type(data.game_id) == "string" then
                game_id = tonumber(data.game_id);
            else
                game_id = data.game_id;
            end;
            if game_id ~=  nil and game_id ~= 0 then 
                local game_state = banner_model.get_game_state(game_id);
                if game_state == nil or game_state.iGame_id == nil or game_state.iGame_id ~= game_id then
                    game_library_control.download_mmog_game(game_id);
                    return;
                end
                base_utility.log("[game_library_control]handle_auto_download: game_state=" .. game_state.iState);
                if game_state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOADING then
                    return;
                elseif game_state.iState == ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED then
                    WGGameAction.continue_download({ game_id_ = game_id });
                elseif game_state.iState == ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED then
                    game_library_control.download_mmog_game(game_id);
                end;
            else 
                return;
            end;
        else
            base_utility.log("[game_library_control]handle_auto_download: param_data= " .. 
                base_utility.table_to_json(data));
            return;
        end;
    end;

    re_download_after_uninstall_broken_game = function(data)
        base_utility.log("[game_library_control][re_download_after_uninstall_broken_game]");
        if data == nil or data.game_id == nil then return end;
        local game_id = data.game_id;
        if state_operation_panel ~= nil then
            base_utility.log("[game_library_control][re_download_after_uninstall_broken_game] redownload start");
            state_operation_panel.download_game_helper(game_id);
            --frame_bottom_control.excute_game_act(GameActions.kDownloadGame, game_id);
        end;
        --game_library_control.download_game(true, game_id);
    end;

    remove_no_auth_game_from_list = function(data)
        if nil == data or nil == data.game_id then
            base_utility.log("[remove_no_auth_game_from_list] input data empty", true);
            return;
        end;
        base_utility.log("[remove_no_auth_game_from_list]game: "..tostring(data.game_id));
        if game_library_model.wait_remove_list_game_id ~= 0 then
            base_utility.log("[remove_no_auth_game_from_list] pre game not remove finish", true);
            return;
        end;

        local game_info = banner_model.get_game_base_info(data.game_id);
        if nil == game_info then
            base_utility.log(
               "[remove_no_auth_game_from_list] base info empty: "..tostring(data.game_id), true);
            return;
        end
        local main_type = game_info.main_type;
        if main_type == nil or main_type ~= game_library_model.game_main_type.CONSOLE_GAME then
            base_utility.log("[remove_no_auth_game_from_list] not console game");
            return;
        end;

        game_library_model.wait_remove_list_game_id = tonumber(data.game_id);
        local post_data = {};
        local game_list = {};
        table.insert(game_list, data.game_id);
        post_data["game_id_list"] = game_list;
        post_data["tgpid"] = wegame_mainframe_model.self_tgp_id;

        local url = LoadStr("RAIL_QUERY_USEGAME_BATCH_URL");
        base_utility.curl_request_async (url, "game_library_control.on_get_remove_game_auth",
                                         false, nil, true, nil, 
                                         base_utility.table_to_json(post_data));
    end;
    
    on_get_remove_game_auth = function(data)
        local game_id = game_library_model.wait_remove_list_game_id;
        game_library_model.wait_remove_list_game_id = 0;

        if data == nil or game_id == nil then
            base_utility.log("[on_get_remove_game_auth]rsp data empty", true);
            return;
        end;
        base_utility.log("[on_get_remove_game_auth]rsp: "..data);
        local result_tb = base_utility.json_to_table(data);
        if nil == result_tb or nil == result_tb.result or 
           0 ~= result_tb.result or
           nil == result_tb.user_game_list then
            -- 回包内容有问题
            base_utility.log("[on_get_remove_game_auth]rsp data error: "..data, true);
            return;
        end

        if false == base_utility.table_is_empty(result_tb.user_game_list) then
            -- 有权限
            base_utility.log("[on_get_remove_game_auth]user has auth");
            return;
        end;

        local game_lib = base_utility.get_component_adapter("IGame_library",
                            "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= game_lib then
            game_lib:add_or_remove_game(game_id, false);
        end
    end;

    on_get_game_list_info = function (data)
        base_utility.log("[game_library_control][on_get_game_list_info]");
        if nil == data then
            return;
        end

        local game_list = game_library_control.get_game_list(data.main_type, data.category, data.sub_type, data.sort);
        local game_list_info = game_library_control.generate_game_info(game_list);
        local game_list_info_json = base_utility.table_to_json(game_list_info);
        local resutl = {};
        resutl["game_list_info_json"] = game_list_info_json;
        return resutl;
    end;

    on_show_dlc_update_failed = function (data)
        local game_id = 0;
        if nil ~=data and nil ~= data.game_id then
            game_id = data.game_id;
        end;
        local param = {
                position = "center",
                title = LoadStr("ID_TPF_UPDATE_DLC_FAILED_TITLE"),
                width = popup_wnd_constants.kPopupWidth,
                height = popup_wnd_constants.kPopupHeight,
                titleIcon = "",
                icon = "",
                tipsIcon = poptips_icons.kError,
                descTitle = LoadStr("ID_TPF_UPDATE_DLC_FAILED_DESC"),
                desc = LoadStr("ID_TPF_UPDATE_DLC_FAILED_SUBDESC"),
                okBtn = {
                    visible = "true",
                    text = LoadStr("ID_TPF_UPDATE_DLC_FAILED_RETRY_LATER_BTN"),
                    close_win = true,
                    handle = function()
                    local handle_failed_param = {game_id = game_id, remove = 0};
                        comm_center.call_service("Svr_RailHost_HandleUpdateDlcFailed",
                        handle_failed_param);
                    end,
                },
                extraBtn = {
                    visible = "true",
                    text = LoadStr("ID_TPF_UPDATE_DLC_FAILED_REMOVE_DLC_BTN"),
                    close_win = true,
                    handle = function()
                        local handle_failed_param = {game_id = game_id, remove = 1};
                        comm_center.call_service("Svr_RailHost_HandleUpdateDlcFailed",
                        handle_failed_param);
                    end,
                },
                cancelBtn = {
                    visible = "false",
                    handle = function()
                    local handle_failed_param = {game_id = game_id, remove = 0};
                        comm_center.call_service("Svr_RailHost_HandleUpdateDlcFailed",
                        handle_failed_param);
                    end,
                },
                game_id = data.game_id,
            }
        ui_utility.show_tips(param);
    end;
}

game_library_control.init();
